package com.woniu.utils;

import com.woniu.vo.TokenVO;
import com.woniu.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplateObject;

    public void expire(String key) {
        redisTemplate.expire(key, DefaultTimeUtil.TOKEN_TIMEOUT, DefaultTimeUtil.TOKEN_UNIT);
    }

    public void delete(String key) {
        Set<String> keys = redisTemplate.keys(key);
        if (!keys.isEmpty()) {
            redisTemplate.delete(keys);
        }
    }

    public void setOpsForValue(String key, TokenVO value) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, DefaultTimeUtil.TOKEN_TIMEOUT, DefaultTimeUtil.TOKEN_UNIT);
    }

    public void setOpsForValue(String key, TokenVO value, Long timeOut, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, timeOut, timeUnit);
    }

    public void setOpsForStringValue(String key, String value, Long timeOut, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, timeOut, timeUnit);
    }


    public void set(String key, String value, Long timeOut, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, DefaultTimeUtil.TOKEN_TIMEOUT, DefaultTimeUtil.TOKEN_UNIT);
    }

    public void setIncrement(String key, long value) {
        redisTemplate.opsForValue().increment(key, value);
    }

    public void setString(String key, Long timeOut) {
        stringRedisTemplate.opsForValue().set(key, "0", timeOut, TimeUnit.MILLISECONDS);
    }

    public void setPermanentString(String key, String value){
        redisTemplate.opsForValue().set(key, value);
    }

    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public TokenVO getOpsForValue(String key) {
        if (StringUtils.isBlank(key) || !redisTemplate.hasKey(key)) {
            return null;
        }
        return (TokenVO) redisTemplate.opsForValue().get(key);
    }

    public String get(String key) {
        if (StringUtils.isBlank(key) || !redisTemplate.hasKey(key)) {
            return null;
        }
        return String.valueOf(redisTemplate.opsForValue().get(key));
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false 不存在
     */
    public boolean exists(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public UserVO getUser(String key) {
        TokenVO tokenVO = getOpsForValue(key);
        if (Objects.isNull(tokenVO)) {
            return null;
        }
        return tokenVO.getUser();
    }

    public String getUserId(String key) {
        UserVO user = getUser(key);
        if (Objects.isNull(user)) {
            return null;
        }
        return user.getUserId();
    }

    /**
     * 列表获取数量
     *
     * @param k string key
     * @return Long
     */
    public Long getListSize(String k) {
        return redisTemplateObject.opsForList().size(k);
    }

    /**
     * 从右边拿出来一个
     *
     * @param k string key
     * @param t Long 超时秒数
     */
    public Object getRightPop(String k, Long t) {
        return redisTemplateObject.opsForList().rightPop(k, t, TimeUnit.SECONDS);
    }

    /**
     * 列表添加左边添加
     *
     * @param k string key
     * @param v Object v
     * @author Mr.Zhang
     * @since 2020-04-13
     */
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplateObject.opsForList();
        list.leftPush(k, v);
    }


}
